package pers.ijfxu.testngext.dataprovider.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Iterator;

import pers.ijfxu.testngext.utils.FileUtils;



/**
 * txt文本迭代器
 *
 * @author ijfxu  
 * @since 2021/1/27 23:52
 */
public class TxtIterator implements Iterator<Object[]> {
    private BufferedReader reader;
    private String nextData;

    /**
     * 构造器
     *
     * @param filePath txt文档路径 
     * @author ijfxu
     * @since 2021/1/27 23:52
     */
    public TxtIterator(String filePath) {
        try {
            this.reader = FileUtils.getReader(filePath);
            this.nextData = reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean hasNext() {
        return null != this.nextData && !this.nextData.isEmpty();
    }

    @Override
    public Object[] next() {
        String linedata;
        if (hasNext()) {
            linedata = nextData;
            try {
                nextData = reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            return null;
        }
        /* 跳过注释符"//"、"#" */
        if (linedata.startsWith("//") || linedata.startsWith("#")) {
            return next();
        }
        return parseData(linedata);
    }

    /**
     * 解析数据，提供继承重写
     *
     * @param data excel map 数据
     * @return Object[] map数据组 
     * @author ijfxu
     * @since 2021/1/27 23:52
     */
    public Object[] parseData(String data) {
        String[] dataArr = new String[1];
        dataArr[0] = data;
        return dataArr;
    }
}
